<?php 
class cls_control_address extends cls_control_base
{
	function get_all_address()
	{
		$address_serve = cls_serve_base::get_instance($this->name);
		if(!isset($_COOKIE['b2c_customerid']))
		{
			$notlogin = return_info(false, "登录超时");
			echo json_encode($notlogin);
			return;
		}
		$customerid = $_COOKIE['b2c_customerid'];
	
		$result = $address_serve->get_all_address($customerid);
	
		echo json_encode($result);
	}
	
	function get_default_address()
	{
		$address_serve = cls_serve_base::get_instance($this->name);
		if(!isset($_COOKIE['b2c_customerid']))
		{
			$notlogin = return_info(false, "登陆超时");
			echo json_encode($notlogin);
			return;
		}
		$customerid = $_COOKIE['b2c_customerid'];
	
		$result = $address_serve->get_default_address($customerid);
	
		echo json_encode($result);
	}
	
	function add_address()
	{
		$address_serve = cls_serve_base::get_instance($this->name);
		if(!isset($_COOKIE['b2c_customerid']))
		{
			$notlogin = return_info(false, "登录超时");
			echo json_encode($notlogin);
			return;
		}
		$customerid = $_COOKIE['b2c_customerid'];
		$postal = $_POST['postal'];
		$receiver = $_POST['receiver'];
		$phone = $_POST['phone'];
		$address = $_POST['address'];
		$description = $_POST['description'];
		
		$postal = mysql_real_escape_string($postal);
		$receiver = mysql_real_escape_string($receiver);
		$phone = mysql_real_escape_string($phone);
		$address = mysql_real_escape_string($address);
		$description = mysql_real_escape_string($description);
	
		if(cls_control_base::check_if_empty($address))
		{
			$message = return_info(false, "地址不能为空");
			echo json_encode($message);
			return;
		}
		if(cls_control_base::check_if_empty($postal))
		{
			$message = return_info(false, "邮编不能为空");
			echo json_encode($message);
			return;
		}
		if(cls_control_base::check_if_empty($receiver))
		{
			$message = return_info(false, "收件人不能为空");
			echo json_encode($message);
			return;
		}
		if(cls_control_base::check_if_empty($phone))
		{
			$message = return_info(false, "电话不能为空");
			echo json_encode($message);
			return;
		}
		
		$result = $address_serve->add_address($customerid, $postal, $receiver, $phone, $address, $description);
	
		echo json_encode($result);
	}
	
	function delete_address()
	{
		$address_serve = cls_serve_base::get_instance($this->name);
		if(!isset($_COOKIE['b2c_customerid']))
		{
			$notlogin = return_info(false, "登录超时");
			echo json_encode($notlogin);
			return;
		}
		$customerid = $_COOKIE['b2c_customerid'];
		$addressid = $_REQUEST['addressid'];
	
		$addressid = mysql_real_escape_string($addressid);
		
		if(cls_control_base::check_if_empty($addressid))
		{
			$message = return_info(false, "没有指定地址ID");
			echo json_encode($message);
			return;
		}
		
		$result = $address_serve->delete_address($customerid, $addressid);
	
		echo json_encode($result);
	}
	
	function update_address()
	{
		$address_serve = cls_serve_base::get_instance($this->name);
		if(!isset($_COOKIE['b2c_customerid']))
		{
			$notlogin = return_info(false, "登录超时");
			echo json_encode($notlogin);
			return;
		}
		$customerid = $_COOKIE['b2c_customerid'];
		$addressid = $_POST['addressid'];
		$postal = $_POST['postal'];
		$receiver = $_POST['receiver'];
		$phone = $_POST['phone'];
		$address = $_POST['address'];
		$description = $_POST['description'];
	
		$addressid = mysql_real_escape_string($addressid);
		$postal = mysql_real_escape_string($postal);
		$receiver = mysql_real_escape_string($receiver);
		$phone = mysql_real_escape_string($phone);
		$address = mysql_real_escape_string($address);
		$description = mysql_real_escape_string($description);
		
		if(cls_control_base::check_if_empty($addressid))
		{
			$message = return_info(false, "没有指定地址ID");
			echo json_encode($message);
			return;
		}
		
		$result = $address_serve->update_address($customerid, $addressid, $postal, $receiver, $phone, $address, $description);
	
		echo json_encode($result);
	}
	
	function set_default_address()
	{
		$address_serve = cls_serve_base::get_instance($this->name);
		if(!isset($_COOKIE['b2c_customerid']))
		{
			$notlogin = return_info(false, "登录超时");
			echo json_encode($notlogin);
			return;
		}
		$customerid = $_COOKIE['b2c_customerid'];
		$addressid = $_REQUEST['addressid'];
	
		$addressid = mysql_real_escape_string($addressid);
		
		if(cls_control_base::check_if_empty($addressid))
		{
			$message = return_info(false, "没有指定地址ID");
			echo json_encode($message);
			return;
		}
		
		$result = $address_serve->set_default_address($customerid, $addressid);
	
		echo json_encode($result);
	}
	
}
?>